package com.zhan.excel_deal.controller;

import com.alibaba.excel.EasyExcel;
import com.zhan.excel_deal.entity.Child;
import com.zhan.excel_deal.entity.Parent;
import com.zhan.excel_deal.listenner.ChildListener;
import com.zhan.excel_deal.listenner.ParentListener;
import com.zhan.excel_deal.util.FileUtil;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class ExcelController {

    @RequestMapping(value = {"/import"}, method = {RequestMethod.POST})
    public void testImport(@RequestParam("files") MultipartFile[] multipartFiles, HttpServletRequest request, HttpServletResponse response) throws IOException {
        ParentListener parentListener = new ParentListener();
        ChildListener childListener = new ChildListener();
        try {
            EasyExcel.read(multipartFiles[0].getInputStream(), Parent.class, parentListener).sheet().doRead();
            EasyExcel.read(multipartFiles[1].getInputStream(), Child.class, childListener).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Parent> parentList = parentListener.getDatas();
        List<Child> childList = childListener.getDatas();
        List<String> list = parentList.parallelStream().collect(Collectors.groupingBy(Parent::getName, Collectors.counting()))
                .entrySet().parallelStream()
                .filter(entry -> entry.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toList());

        for (Child child : childList) {
            if (child.getId() == null) {
                continue;
            }
            for (Parent parent : parentList) {
                if (child.getName().equals(parent.getName()) && child.getId().equals(parent.getId())) {
                    child.setNumber(parent.getNumber());
                    child.setPhone(parent.getPhone());
                    child.setDate(parent.getDate());
                }
            }
        }
        ServletOutputStream out = response.getOutputStream();
        try {
            File file = FileUtil.multipartFileToFile(multipartFiles[1]);
            FileInputStream fs = new FileInputStream(file);
            if (file.getName().contains("xlsx")) {
                XSSFWorkbook wb = new XSSFWorkbook(fs);
                XSSFCellStyle style = wb.createCellStyle();
                XSSFFont font = wb.createFont();
                XSSFSheet sheet = wb.getSheetAt(0);
                int lastRowNum = sheet.getLastRowNum();
                int colmNum = sheet.getRow(0).getPhysicalNumberOfCells();
                int x = 1;
                int z = 3;
                int s = 4;
                int r = 11;
                for (int i = 0; i < colmNum; i++) {
                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("*姓名")) {
                        x = i;
                    }

                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("*证件号码")) {
                        z = i;
                    }

                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("*手机号码")) {
                        s = i;
                    }

                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("任职受雇从业日期")) {
                        r = i;
                    }

                }
                for (int i = 1; i <= lastRowNum; i++) {
                    for (Child child : childList) {
                        if (child.getId() == null) {
                            continue;
                        }
                        if (sheet.getRow(i).getCell(x).getStringCellValue().equals(child.getName()) && sheet.getRow(i).getCell(r).getStringCellValue().equals(child.getId())) {
                            font.setFontHeightInPoints((short) 12);
                            font.setFontName("宋体");
                            style.setFont(font);
                            if (sheet.getRow(i).getCell(z) == null) {
                                sheet.getRow(i).createCell(z).setCellValue("");
                            }
                            if (sheet.getRow(i).getCell(s) == null) {
                                sheet.getRow(i).createCell(s).setCellValue("");
                            }
                            sheet.getRow(i).getCell(z).setCellStyle(style);
                            sheet.getRow(i).getCell(s).setCellStyle(style);
                            sheet.getRow(i).getCell(r).setCellStyle(style);
                            sheet.getRow(i).getCell(z).setCellValue(child.getNumber());
                            sheet.getRow(i).getCell(s).setCellValue(child.getPhone());
                            sheet.getRow(i).getCell(s).setCellValue(child.getDate());
                        }
                        if (list.contains(child.getName())) {
                            style.setFillBackgroundColor((short) 1);
                            sheet.getRow(i).getCell(x).setCellStyle(style);
                        }
                    }
                }
                fs.close();
                FileUtil.delteTempFile(file);
                out.flush();
                wb.write(out);
                fs.close();
            } else {
                HSSFWorkbook wb = new HSSFWorkbook(fs);
                HSSFCellStyle style = wb.createCellStyle();
                HSSFFont font = wb.createFont();
                HSSFSheet sheet = wb.getSheetAt(0);
                int lastRowNum = sheet.getLastRowNum();
                int colmNum = sheet.getRow(0).getPhysicalNumberOfCells();
                int x = 1;
                int z = 3;
                int s = 4;
                int r = 11;
                for (int i = 0; i < colmNum; i++) {
                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("*姓名")) {
                        x = i;
                    }

                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("*证件号码")) {
                        z = i;
                    }

                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("手机号码")) {
                        s = i;
                    }

                    if (sheet.getRow(0).getCell(i).getStringCellValue().equals("任职受雇从业日期")) {
                        r = i;
                    }

                }
                for (int i = 1; i <= lastRowNum; i++) {
                    if (sheet.getRow(i).getCell(x) == null) {
                        sheet.getRow(i).createCell(x).setCellValue("");
                    }
                    if (sheet.getRow(i).getCell(0) == null) {
                        sheet.getRow(i).createCell(0).setCellValue("");
                    }
                    if (sheet.getRow(i).getCell(z) == null) {
                        sheet.getRow(i).createCell(z).setCellValue("");
                    }
                    if (sheet.getRow(i).getCell(s) == null) {
                        sheet.getRow(i).createCell(s).setCellValue("");
                    }
                    if (sheet.getRow(i).getCell(r) == null) {
                        sheet.getRow(i).createCell(r).setCellValue("");
                    }
                    sheet.getRow(i).getCell(x).setCellType(CellType.STRING);
                    sheet.getRow(i).getCell(0).setCellType(CellType.STRING);
                    for (Child child : childList) {
                        if (sheet.getRow(i).getCell(x).getStringCellValue().equals(child.getName()) && sheet.getRow(i).getCell(0).getStringCellValue().equals(child.getId())) {
                            font.setFontHeightInPoints((short) 12);
                            font.setFontName("宋体");
                            style.setFont(font);
                            sheet.getRow(i).getCell(z).setCellStyle(style);
                            sheet.getRow(i).getCell(s).setCellStyle(style);
                            sheet.getRow(i).getCell(r).setCellStyle(style);
                            sheet.getRow(i).getCell(z).setCellValue(child.getNumber());
                            sheet.getRow(i).getCell(s).setCellValue(child.getPhone());
                            sheet.getRow(i).getCell(r).setCellValue(child.getDate());
                        }
                        if (list.contains(child.getName())) {
                            style.setFillBackgroundColor((short) 1);
                            sheet.getRow(i).getCell(x).setCellStyle(style);
                        }
                    }
                }
                fs.close();
                FileUtil.delteTempFile(file);
                out.flush();
                wb.write(out);
                fs.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

